﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace BenhVien
{
    public partial class BenhNhanNgoaiTru : Form
    {
        private SqlDataAdapter da = new SqlDataAdapter();
        private DataTable dt = new DataTable();
        private Connect connec = new Connect();
        public BenhNhanNgoaiTru()
        {
            InitializeComponent();
        }

        private void BenhNhanNgoaiTru_Load(object sender, EventArgs e)
        {
            
        }

        private void BenhNhanNgoaiTru_Load_1(object sender, EventArgs e)
        {
            connec.connect();
            getdata();
            binding();
        }
        public void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = connec.con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select * from BenhNhanNgoaiTru";
            da.SelectCommand = command;
            da.Fill(dt);
            dataGridView1.DataSource = dt;
        }
        public void binding()
        {
            txtKhu.DataBindings.Clear();
            txtKhu.DataBindings.Add("Text", dataGridView1.DataSource, "Khu");
            txtMBN.DataBindings.Clear();
            txtMBN.DataBindings.Add("Text", dataGridView1.DataSource, "MaBN");
            txtMNV.DataBindings.Clear();
            txtMNV.DataBindings.Add("Text", dataGridView1.DataSource, "MaNV");
            d1.DataBindings.Clear();
            d1.DataBindings.Add("Text", dataGridView1.DataSource, "ThoiGianKham");
            txtPK.DataBindings.Clear();
            txtPK.DataBindings.Add("Text", dataGridView1.DataSource, "PhongKham");
        }

        private void bt_them_Click(object sender, EventArgs e)
        {
            if (txtKhu.Text == "" | txtMBN.Text == "" | txtMNV.Text == "" | txtPK.Text == "" | d1.Text == "")
                MessageBox.Show("Bạn Chưa Nhập Đủ Thông Tin", "Thông Báo", MessageBoxButtons.OK);
            else
            {
                if (check("Select MaBN from BenhNhanNgoaiTru where MaBN =@DoiSo", txtMBN.Text) == true)
                    MessageBox.Show("Ma bi trung", "Loi", MessageBoxButtons.OK);
                else
                {
                    if (check("Select MaBN from BenhNhan where MaBN =@DoiSo", txtMBN.Text) == false)
                        MessageBox.Show("Ma Benh Nhan Khong Ton Tai", "Loi", MessageBoxButtons.OK);
                    else
                    {
                        if (check("select MaNV from NhanVien where MaNV = @DoiSo ", txtMNV.Text) == false)
                            MessageBox.Show("Mã nhân viên không tồn tại", "lỗi", MessageBoxButtons.OK);
                        else
                        {
                            DataRow row = dt.NewRow();
                            row["MaBN"] = txtMBN.Text;
                            row["MaNV"] = txtMNV.Text;
                            row["Khu"] = txtKhu.Text;
                            row["ThoiGianKham"] = d1.Value;
                            row["PhongKham"] = txtPK.Text;
                            dt.Rows.Add(row);
                            SqlCommand commandInsert = new SqlCommand();
                            commandInsert.Connection = connec.con;
                            commandInsert.CommandType = CommandType.Text;
                            commandInsert.CommandText = @"Insert BenhNhanNgoaiTru (MaBN, Khu, MaNV, ThoiGianKham, PhongKham) Values 
                                                ( @MaBN, @Khu,  @MaNV, @ThoiGianKham, @PhongKham)";
                            commandInsert.Parameters.Add("@MaBN", SqlDbType.NVarChar, 50).Value = txtMBN.Text;
                            commandInsert.Parameters.Add("@Khu", SqlDbType.NVarChar, 50).Value = txtKhu.Text;
                            commandInsert.Parameters.Add("@MaNV", SqlDbType.NVarChar, 50).Value = txtMNV.Text;
                            commandInsert.Parameters.Add("@ThoiGianKham", SqlDbType.Date, 50).Value = d1.Value;
                            commandInsert.Parameters.Add("@PhongKham", SqlDbType.NVarChar, 50).Value = txtPK.Text;
                            da.InsertCommand = commandInsert;
                            da.Update(dt);
                            MessageBox.Show("Ban da them Thanh Cong", "chuc Mung", MessageBoxButtons.OK);
                            
                        }
                    }
                }
            }
        }
        private bool check(String s, String s1)
        {
            bool check = false;
            DataTable data = new DataTable();
            SqlCommand command = new SqlCommand();
            command.Connection = connec.con;
            command.CommandType = CommandType.Text;
            command.CommandText = s;
            command.Parameters.Add("@DoiSo", SqlDbType.NVarChar, 50).Value = s1;
            da.SelectCommand = command;
            da.Fill(data);
            if (data.Rows.Count > 0) check = true;
            return check;
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            DataRow row = dt.Select("MaBN like '" + txtMBN.Text + "'")[0];
            row.BeginEdit();
            row.Delete();
            row.EndEdit();
            SqlCommand commandDelete = new SqlCommand();
            commandDelete.Connection = connec.con;
            commandDelete.CommandType = CommandType.Text;
            commandDelete.CommandText = @"Delete from BenhNhanNgoaiTru Where MaBN=@MaBN";
            commandDelete.Parameters.Add("@MaBN", SqlDbType.NVarChar, 50, "MaBN");
            da.DeleteCommand = commandDelete;
            da.Update(dt);
            MessageBox.Show("ban da xóa thanh cong", "chuc mung", MessageBoxButtons.OK);
 
        }

        private void bt_Luu_Click(object sender, EventArgs e)
        {
            DataRow row = dt.Select("MaBN = '" + txtMBN.Text + "'")[0];
            row.BeginEdit();
            row["MaBN"] = txtMBN.Text;
            row["Khu"] = txtKhu.Text;
            row["MaNV"] = txtMNV.Text;
            row["ThoiGianKham"] = d1.Value;
            row["PhongKham"] = txtPK.Text;
            row.EndEdit();
            SqlCommand commandUpdate = new SqlCommand();
            commandUpdate.Connection = connec.con;
            commandUpdate.CommandType = CommandType.Text;
            commandUpdate.CommandText = @"Update BenhNhanNgoaiTru Set 
                                                              Khu= @Khu,
                                                              MaNV= @MaNV,
                                                              ThoiGianKham=@ThoiGianKham,
                                                              PhongKham=@PhongKham
                                                              where MaBN= @MaBN";
            commandUpdate.Parameters.Add("@MaBN", SqlDbType.NVarChar, 50, "MaBN");
            commandUpdate.Parameters.Add("@Khu", SqlDbType.NVarChar, 50, "Khu");
            commandUpdate.Parameters.Add("@MaNV", SqlDbType.NVarChar, 50, "MaNV");
            commandUpdate.Parameters.Add("@ThoiGianKham", SqlDbType.Date, 50, "ThoiGianKham");
            commandUpdate.Parameters.Add("@PhongKham", SqlDbType.NVarChar, 50, "PhongKham");
            da.UpdateCommand = commandUpdate;
            da.Update(dt);
            MessageBox.Show("ban da chinh sua thanh cong", "chuc mung", MessageBoxButtons.OK);
        }

        private void bt_NhapMoi_Click(object sender, EventArgs e)
        {
            txtPK.Text = ""; txtMNV.Text = ""; txtMBN.Text = "";
            txtKhu.Text = ""; d1.Text = ""; txtMBN.Enabled = true;
            bt_Luu.Enabled = false;
            bt_Xoa.Enabled = false;
        }

        private void bt_Reset_Click(object sender, EventArgs e)
        {
            txtMBN.Enabled = false;
            txtMNV.Enabled=false;
            bt_Xoa.Enabled = true;
            bt_Luu.Enabled = true;
            dt.Clear();
            getdata();
        }
    }
}
